package cn.dada.dadarecipes.community.favorites.service.impl;

import cn.dada.dadarecipes.base.exception.ServiceException;
import cn.dada.dadarecipes.base.response.StatusCode;
//import cn.dada.dadarecipes.base.util.LoginValidation;
import cn.dada.dadarecipes.community.article.mapper.ArticleMapper;
import cn.dada.dadarecipes.community.favorites.mapper.FavoritesMapper;
import cn.dada.dadarecipes.community.favorites.pojo.dto.FavoritesRemoveParam;
import cn.dada.dadarecipes.community.favorites.pojo.dto.FavoritesSaveParam;
import cn.dada.dadarecipes.community.favorites.pojo.entity.Favorites;
import cn.dada.dadarecipes.community.favorites.pojo.vo.FavoritesVO;
import cn.dada.dadarecipes.community.favorites.service.FavoritesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@Transactional
public class FavoritesServiceImpl implements FavoritesService {
    @Autowired
    private FavoritesMapper favoritesMapper;
    @Autowired
    private ArticleMapper articleMapper;


    /**
     * 业务层-查询收藏列表
     * @param userId
     * @param session
     * @return
     */
    @Override
    public List<FavoritesVO> selectFavorites(Long userId, HttpSession session) {
        log.debug("业务层-查询收藏列表: userId:{},{}",userId,session);
        /** 用户是否为当前用户登录校验 */
//        LoginValidation.isMyselfLogin(userId, session);

        List<FavoritesVO> list = favoritesMapper.selectFavorites(userId);
        log.debug("业务层-查询收藏列表-查询结果数: {}",list.size());
        if (list.isEmpty()) throw new ServiceException(StatusCode.FAVORITES_IS_EMPTY);// 收藏为空

        return list;
    }



    /**
     * 业务层-添加收藏
     * @param favoritesSaveParam
     * @param session
     */
    @Override
    public void saveFavorites(FavoritesSaveParam favoritesSaveParam, HttpSession session) {
        log.debug("业务层-添加收藏: favoritesSaveParam:{},{}", favoritesSaveParam,session);
        /* 用户是否为当前用户登录校验 */
//        LoginValidation.isMyselfLogin(favoritesSaveParam.getUserId(), session);

        /* 判断收藏的文章是否存在 */
        Long articleId = articleMapper.selectArticleId(favoritesSaveParam.getArticleId());
        log.debug("业务层-添加收藏-查询文章是否存在: {}",articleId);
        if (articleId == null) throw new ServiceException(StatusCode.ARTICLE_NOT_EXIST);// 文章不存在

        /* 判断文章是否已经收藏 */
        Long favoritesId = favoritesMapper.selectFavoritesArticleId(favoritesSaveParam);
        log.debug("业务层-添加收藏-查询文章是否已经收藏: {}",favoritesId);
        if (favoritesId != null) throw new ServiceException(StatusCode.FAVORITE_ALREADY_EXIST);// 文章已经收藏

        /* 添加收藏 */
        Favorites favorites = new Favorites();
        BeanUtils.copyProperties(favoritesSaveParam, favorites);
        favorites.setFavoriteTime(LocalDateTime.now());
        Boolean isSuccess = favoritesMapper.insertFavorites(favorites);
        log.debug("业务层-添加收藏-添加文章是否成功: {}",isSuccess);
        if (!isSuccess) throw new ServiceException(StatusCode.OPERATION_FAILED);// 操作失败
    }



    /**
     * 业务层-取消收藏
     * @param favoritesRemoveParam
     * @param session
     */
    @Override
    public void removeFavorites(FavoritesRemoveParam favoritesRemoveParam, HttpSession session) {
        log.debug("业务层-取消收藏: favoritesRemoveParam:{},{}", favoritesRemoveParam,session);
        /** 用户是否为当前用户登录校验 */
//        LoginValidation.isMyselfLogin(favoritesRemoveParam.getUserId(), session);

        /** 取消收藏 */
        Boolean isSuccess = favoritesMapper.deleteFavorites(favoritesRemoveParam);
        log.debug("业务层-取消收藏-取消收藏是否成功: {}",isSuccess);
        if (!isSuccess) throw new ServiceException(StatusCode.OPERATION_FAILED);// 操作失败
    }
}
